package com.api.gbhg.daiyanlu.Dao;

import weaver.conn.RecordSet;

public class IndependentFinanceDao {
    public  static RecordSet getMainTable(Integer pageNum,Integer pageSize,String nd,String dw,int state){
        RecordSet rs = new RecordSet();
        StringBuilder sb = new StringBuilder();
        if (dw.equals("2")){
            sb.append("select jf.xmmc,jf.sbdw,SUM(jf.ze) ze,SUM(jf.syed) syed,jf.requestId from " +
                    " uf_jingfei jf join uf_jingfei_version jv on jf.requestId = jv.requestId \n" +
                    "where jf.nd = "+nd+" and jv.sfwdlhsdw = "+state+" group by jf.requestId,jf.xmmc,jf.sbdw  ");
        }else{
            sb.append("select jf.xmmc,jf.sbdw,SUM(jf.ze) ze,SUM(jf.syed) syed,jf.requestId from " +
                    " uf_jingfei jf join uf_jingfei_version jv on jf.requestId = jv.requestId \n" +
                    "where jf.nd = "+nd+" and jv.sfwdlhsdw = "+state+" and jf.sbdw in ("+dw+") group by jf.requestId,jf.xmmc,jf.sbdw  ");
        }
            pageNum=(pageNum-1)*pageSize;
            if("sqlserver".equals(rs.getDBType())){
                sb.append(" order by requestId offset "+pageNum+" rows fetch next "+pageSize+" rows only");
            }else if("mysql".equals(rs.getDBType())){
                sb.append(" LIMIT "+pageNum+","+pageSize+"");
            }
        rs.executeQuery(sb.toString());
        return rs;
    }

    //获取数据条数
    public static int getMainTableCount(String nd,String dw,int state) {
        RecordSet rs = new RecordSet();
        String sql;
        if (dw.equals("2")) {
            sql = "select COUNT(requestId) cut from \n" +
                    "(select jf.requestId from uf_jingfei jf join uf_jingfei_version jv on jf.requestId = jv.requestId \n" +
                    "where  jf.nd = " + nd + " and jv.sfwdlhsdw = " + state + "  group by jf.requestId ) tb  ";
        } else {
            sql = "select COUNT(requestId) cut from \n" +
                    "(select jf.requestId from uf_jingfei jf join uf_jingfei_version jv on jf.requestId = jv.requestId \n" +
                    "where  jf.nd = " + nd + " and jv.sfwdlhsdw = " + state + " and jf.sbdw in (" + dw + ") group by jf.requestId ) tb  ";
        }
        rs.executeQuery(sql);
        rs.next();
        return rs.getInt("cut");
    }
}
